/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBeans;

import facade.SuiLibFacade;
import facade.exception.NotExistingObjectException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import valueObject.ProfessorVO;
import valueObject.RoleVO;
import valueObject.UniversityVO;
import valueObject.UserVO;

/**
 *
 * @author David
 */
public class professorMB {

    private static final int PROFESSOR_ROLE = 3;
    private UserVO userVO;
    private boolean success = false;
    private String name;
    private String surname;
    private String email;
    private String username;
    private String password;
    private String nationality;
    private String web;
    private String description;
    
    private List<ProfessorVO> professors;
    
    @EJB
    private SuiLibFacade suilibFacade;
    
    /**
     * Creates a new instance of professorMB
     */
    public professorMB() {
    }

      /**
     * @return the userVO
     */
    public UserVO getUserVO() {
        return userVO;
    }

    /**
     * @param userVO the userVO to set
     */
    public void setUserVO(UserVO userVO) {
        this.userVO = userVO;
    }
    
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the surname
     */
    public String getSurname() {
        return surname;
    }

    /**
     * @param surname the surname to set
     */
    public void setSurname(String surname) {
        this.surname = surname;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
    
    public void createProfessor() {
        try {
            UserVO UserVO = new UserVO();
            UserVO.setUsername(username);
            UserVO.setPassword(password);
            RoleVO roleVO = new RoleVO();
            roleVO.setId(PROFESSOR_ROLE);
            UserVO.setRole(roleVO);
            suilibFacade.createUser(UserVO);
            ProfessorVO professorVO =  new ProfessorVO();
            professorVO.setUserName(username);
            professorVO.setName(name);
            professorVO.setSurname(surname);
            professorVO.setEmail(email);
            UserVO userVo = (UserVO) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");
            UniversityVO universityVo = suilibFacade.findUniversity(userVo.getUsername());
            professorVO.setUniversity(universityVo);
            suilibFacade.createProfessor(professorVO);
            success = true;
        } catch (NotExistingObjectException ex) {
            Logger.getLogger(professorMB.class.getName()).log(Level.SEVERE, null, ex);
            success = false;
        }
    }

    /**
     * @return the professors
     */
    public List<ProfessorVO> getProfessors() {
        try {
            String sesionUser = ((UserVO) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user")).getUsername();
            UniversityVO universityVo = suilibFacade.findUniversity(sesionUser);
            return suilibFacade.findProfessorsByUniversity(universityVo);
        } catch (NotExistingObjectException ex) {
            Logger.getLogger(professorMB.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    /**
     * @return the nationality
     */
    public String getNationality() {
        return nationality;
    }

    /**
     * @param nationality the nationality to set
     */
    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

    /**
     * @return the web
     */
    public String getWeb() {
        return web;
    }

    /**
     * @param web the web to set
     */
    public void setWeb(String web) {
        this.web = web;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }
    
    public void editProfessor() {
        try {
            UserVO userVo = (UserVO) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");
            ProfessorVO professorVO = suilibFacade.findProfessorVOById(userVo.getUsername());
            professorVO.setName(name);
            professorVO.setSurname(surname);
            professorVO.setEmail(email);
            professorVO.setNationality(nationality);
            professorVO.setWeb(web);
            professorVO.setDescription(description);
            suilibFacade.updateProfessor(professorVO);
        } catch (NotExistingObjectException ex) {
            Logger.getLogger(universitiesMB.class.getName()).log(Level.SEVERE, null, ex);
        }
      
    }

    /**
     * @return the success
     */
    public boolean isSuccess() {
        return success;
    }
}
